﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using xTeam.HengLe.Entity;
using xTeam.Hengle.DataAccess;
using xTeam.HengLe.Helper;
using System.Web.Mvc.Html;

namespace xTeam.HengLe.Controller.Manage
{
    public class ClassifyController : BaseController
    {

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public ActionResult Index(string moduleKey)
        {
            ViewData["moduleKey"] = moduleKey;
            List<EClassify> classifyList = _classifyDataAccess.GetClassLisytByModuleKey(moduleKey);
            return View("Classify", classifyList);
        }
        /// <summary>
        /// 获取指定的的分类的详细信息
        /// </summary
        public ActionResult Classify(int classifyId = 0, string moduleKey = "")
        {
            EClassify classify =
                _classifyDataAccess.GetClassifyById(classifyId)
                ?? new EClassify() { ModuleKey = moduleKey };
            List<EClassify> classifyList = _classifyDataAccess.GetClassLisytByModuleKey(moduleKey);
            ViewData["ClassifyList"] = classifyList;

            #region 探测ModuleKey的有效性

            List<SelectListItem> classifyModuleKey = new List<SelectListItem>();
            classifyModuleKey.Add(new SelectListItem() { Text = "", Value = "" });
            classifyModuleKey.Add(new SelectListItem() { Text = "产 品", Value = "Product" });
            classifyModuleKey.Add(new SelectListItem() { Text = "新 闻", Value = "News" });
            classifyModuleKey.Add(new SelectListItem() { Text = "系 统", Value = "System" });
            classifyModuleKey.Add(new SelectListItem() { Text = "Product", Value = "Product_en" });
            classifyModuleKey.Add(new SelectListItem() { Text = "News", Value = "News_en" });
            classifyModuleKey.Add(new SelectListItem() { Text = "System", Value = "System_en" });
            classify.ModuleKey = classifyModuleKey.Exists(i => i.Value.ToUpper() == classify.ModuleKey.ToUpper())
                 ? classify.ModuleKey
                 : string.Empty;
            ViewData["ModuleKey"] = new SelectList(classifyModuleKey, "Value", "Text", classify.ModuleKey);
            this.ModelState.Remove("ModuleKey");
            #endregion

         
            return View("Post", classify);
        }
        /// <summary>
        /// 保存分类
        /// </summary>
        public ActionResult Save(EClassify classify)
        {
            int result = _classifyDataAccess.Save(classify);
            return new EmptyResult();
        }
        /// <summary>
        /// 删除分类
        /// </summary>
        public ActionResult Delete(int classifyId = 0)
        {
            int result = _classifyDataAccess.Delete(classifyId);
            return new EmptyResult();
        }
    }
}
